package dreamCookGame;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import dreamCookGame.chickenMiniGame.ChickenMiniGame;
import dreamCookGame.chickenMiniGame.Entity;

public class MainScreen extends JPanel {
	int _width;
	int _height;
	Scene _currentScene;
	Menu _bottomMenu;
	BufferedImage _finalImg;
	Graphics2D _g2;
	References _reference;
	Video _currentVideo;
	boolean _videoOn;
	JLabel _gameLabel;
	
	public MainScreen(int width, int height) {
		_width = width;
		_height = height;
		_finalImg = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB);
		_reference = new References();
		
		//This lets us set the layout of the components precisely
		//(thus removing the small padding at the top)
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(0,0,0,0);
		_gameLabel = new JLabel(new ImageIcon(_finalImg),JLabel.LEFT);
		this.add(_gameLabel);
		
		_g2 = (Graphics2D)_finalImg.createGraphics();
		
		_videoOn = false;
	}
	
	public void displayVideo(Video video){
		_videoOn = true;
		_currentVideo = video;
		_currentVideo.loadResources(_reference);
		this.remove(_gameLabel);
		setLayout( new BorderLayout() );
		add(_currentVideo._videoComponent, BorderLayout.CENTER);
		_currentVideo.restart();
		_currentVideo.play();
	}

	public void stopVideo(){
		_currentVideo.stop();
		this.remove(_currentVideo._videoComponent);
		this.add(_gameLabel);
		
		//_g2 = (Graphics2D)_finalImg.createGraphics();
		_videoOn = false;
	}
	
	public void loadScene(Scene scene, Menu bottomMenu) {
		//TODO: Free resources from last scene (Analia)
		_currentScene = scene;
		_currentScene.loadResources(_reference);
		_bottomMenu = bottomMenu;
		
		if(bottomMenu != null)
			loadBottomMenu(bottomMenu);
	}
	
	public Scene getScene() {
		return _currentScene;
	}
	
	public void loadBottomMenu(Menu bottomMenu) {
		_bottomMenu = bottomMenu;
		_bottomMenu.loadResources(_reference);
	}
	
	public void constructFinalImg() {
		if( _videoOn == false ) {
			clearAndDrawBackground();
		
			drawItemsBack();
		
			drawCharacters();
		
			drawForeground();
		
			drawItemsFront();
		
			drawItemMenus();
		
			drawBottomMenu();
		}
	}

	public void clearAndDrawBackground() {
		_g2.clearRect(0, 0, _width, _height);
		_g2.drawImage(_currentScene._backgroundImg, 0, 0, this);
	}
	
	public void drawItemsBack() {	
		if(_currentScene._itemsBack != null)
			for(int i = 0; i < _currentScene._itemsBack.length; i++){
				_g2.drawImage(_currentScene._itemsBack[i]._itemButton._img, _currentScene._itemsBack[i]._x, _currentScene._itemsBack[i]._y, this);
			}
		if(_currentScene._backgroundImgName.equalsIgnoreCase("chickenMiniGameBackground")){
			Entity[] tempArray = ((ChickenMiniGame)_currentScene)._entities;
			for(int i=0; i<tempArray.length;i++){
				AffineTransform tempTransform = _g2.getTransform();
				_g2.translate((int)tempArray[i]._location.getX(), (int)tempArray[i]._location.getY());
				_g2.rotate(-tempArray[i].getDirection());
				_g2.translate(-64, -64);
				_g2.drawImage(tempArray[i]._sprite[(int)tempArray[i]._currentFrame],0,0,this);
				_g2.setTransform(tempTransform);
			}
		}
	}
	
	public void drawItemsFront() {	
		if(_currentScene._itemsFront != null)
			for(int i = 0; i < _currentScene._itemsFront.length; i++){
				_g2.drawImage(_currentScene._itemsFront[i]._itemButton._img, _currentScene._itemsFront[i]._x, _currentScene._itemsFront[i]._y, this);
			}
	}
	
	public void drawItemMenus() {
		if(_currentScene._itemsBack != null)
			for(int i = 0; i < _currentScene._itemsBack.length; i++){
				Item tempItem = _currentScene._itemsBack[i];
				if(tempItem._menuOn){
					_g2.drawImage(tempItem._itemMenu._img,tempItem._itemMenu._x,tempItem._itemMenu._y,this);
					for(int j = 0; j < tempItem._itemMenu._itemMenuButtons.length; j++)
						_g2.drawImage(tempItem._itemMenu._itemMenuButtons[j]._img, tempItem._itemMenu._itemMenuButtons[j]._x, tempItem._itemMenu._itemMenuButtons[j]._y, this);
				}
		}
		if(_currentScene._itemsFront != null)
			for(int i = 0; i < _currentScene._itemsFront.length; i++){
				Item tempItem = _currentScene._itemsFront[i];
				if(tempItem._menuOn){
					_g2.drawImage(tempItem._itemMenu._img, tempItem._itemMenu._x,tempItem._itemMenu._y,this);
					for(int j = 0; j < tempItem._itemMenu._itemMenuButtons.length; j++)
						_g2.drawImage(tempItem._itemMenu._itemMenuButtons[j]._img, tempItem._itemMenu._x, tempItem._itemMenu._y, this);
				}
		}
		if(_currentScene._characters != null)
			for(int i = 0; i < _currentScene._characters.length; i++){
				Character tempCharacter = _currentScene._characters[i];
				if(tempCharacter._menuOn){
					_g2.drawImage(tempCharacter._itemMenu._img, tempCharacter._itemMenu._x, tempCharacter._itemMenu._y,this);
					for(int j = 0; j < tempCharacter._itemMenu._itemMenuButtons.length; j++)
						_g2.drawImage(tempCharacter._itemMenu._itemMenuButtons[j]._img, tempCharacter._itemMenu._itemMenuButtons[j]._x, tempCharacter._itemMenu._itemMenuButtons[j]._y, this);
				}
		}
	}
	
	
	
	public void drawCharacters() {
		if(_currentScene._characters != null)
			for(int i = 0; i < _currentScene._characters.length; i++)
				_g2.drawImage(_currentScene._characters[i]._img, _currentScene._characters[i]._x, _currentScene._characters[i]._y, this);			
			
	}    
	
	public void drawForeground() {
		if(_currentScene._foregroundImg != null)
			_g2.drawImage(_currentScene._foregroundImg, _currentScene._foregroundX, _currentScene._foregroundY, this);
	}
	
	public void drawBottomMenu() {
		if(_bottomMenu != null) {
			_g2.drawImage(_bottomMenu._img, (int)_bottomMenu._boundingBox.getX(), (int)_bottomMenu._boundingBox.getY(), this);
			
			for(int i = 0; i < _bottomMenu._buttons.length; i++)
			{
			//	if(_bottomMenu._buttons[i]._changed) {
					_g2.drawImage(_bottomMenu._buttons[i]._img, (int)_bottomMenu._buttons[i]._x, (int)_bottomMenu._buttons[i]._y, this);
					_bottomMenu._buttons[	i]._changed = false;
				//}
			}
		}
	}
	
	public void redraw() {
		constructFinalImg();
		this.validate();
		this.repaint();
	}	
}
